package cz.czu.pef.DataModelling.entityObjects;

import javax.persistence.*;

/**
 * This entity represents concrete instance of MachineryType.
 * For example concrete tractor, concrete building crane, etc.
 * 
 * @author rstribrn
 *
 */
@Entity
@Table(name="MACHINERY", uniqueConstraints=@UniqueConstraint(columnNames="SERIAL_NUMBER", name="UQ_MACHINERY_SERIAL_NUMBER"))
@Inheritance(strategy=InheritanceType.JOINED)
@NamedQueries({
	@NamedQuery(name="findMachineryBySerialNo", query="SELECT m FROM MachineryEntityImpl m WHERE m.machinerySerialNo = :serialNo")
})
public class MachineryEntityImpl extends IdPersistenceEntityObjectImpl implements IdPersistenceEntityObject
{
	private static final long serialVersionUID = 6497514274059955169L;

	/*
	 * Entity attributes
	 */
	@Column(name="SERIAL_NUMBER", length=32, nullable=false)
	private String machinerySerialNo;

	/*
	 * Relations
	 */
	@ManyToOne(fetch=FetchType.EAGER, optional=true, targetEntity=CompanyEntityImpl.class) // no cascade
	@JoinTable(name="MAP_MACHINERY_COMPANY")
	private CompanyEntityImpl companyOwner;

	@ManyToOne(fetch=FetchType.EAGER, optional=true, targetEntity=PersonEntityImpl.class) // no cascade
	@JoinTable(name="MAP_MACHINERY_PERSON")
	private PersonEntityImpl personOwner;

	@ManyToOne(fetch=FetchType.EAGER, optional=true, targetEntity=MachineTypeEntityImpl.class) // no cascade
	@JoinTable(name="MAP_MACHINERY_MACHINE_TYPE")
	private MachineTypeEntityImpl machineType;

	public MachineryEntityImpl(String machineSerialNo, MachineTypeEntityImpl machineType, PersonEntityImpl personOwner, CompanyEntityImpl companyOwner)
	{
		super();
		this.setMachineType(machineType);
		this.setPersonOwner(personOwner);
		this.setCompanyOwner(companyOwner);
		this.machinerySerialNo = machineSerialNo;
	}

	public MachineryEntityImpl()
	{
		super();
	}

	public MachineryEntityImpl(String machineSerialNo)
	{
		super();
		this.machinerySerialNo = machineSerialNo;
	}

	public void setMachineSerialNo(String machineSerialNo)
	{
		this.machinerySerialNo = machineSerialNo;
	}

	public String getMachineSerialNo()
	{
		return machinerySerialNo;
	}
	
	public MachineTypeEntityImpl getMachineType()
	{
		return machineType;
	}
	
	public void setMachineType(MachineTypeEntityImpl machineType)
	{
		this.machineType = machineType;
	}
	
	public PersonEntityImpl getPersonOwner()
	{
		return personOwner;
	}
	
	public void setPersonOwner(PersonEntityImpl personOwner)
	{
		this.personOwner = personOwner;
	}

	public CompanyEntityImpl getCompanyOwner()
	{
		return companyOwner;
	}
	
	public void setCompanyOwner(CompanyEntityImpl companyOwner)
	{
		this.companyOwner = companyOwner;
	}

    
  public String getMachinerySerialNo()
  {
    return machinerySerialNo;
  }

  
  public void setMachinerySerialNo(String machinerySerialNo)
  {
    this.machinerySerialNo = machinerySerialNo;
  }

    @Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("MachineryEntityImpl [machineSerialNo=");
		builder.append(machinerySerialNo);
		builder.append(", machineType=");
		builder.append(machineType);
		builder.append(", toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}

	/* (non-Javadoc)
	 * @see cz.czu.pef.DataModelling.domainObjects.Machinery#canBeRemoved()
	 */
	@Override
	public boolean canBeRemoved()
	{
		return true;
	}
}
